/** 
 * <p>Original Author:  jessefreeman</p>
 * <p>Class File: ComponentEvent.as</p>
 * 
 * <p>Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:</p>
 * 
 * <p>The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.</p>
 * 
 * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.</p>
 * 
 * <p>Licensed under The MIT License</p>
 * <p>Redistributions of files must retain the above copyright notice.</p>
 *
 * <p>Revisions<br/> 
 * 	0.1  Initial version Oct 9, 2008</p>
 *
 */
 
package camo.views.components.buttons
{
	import camo.events.ComponentEvent;
	import camo.models.IModel;

	public class TextToggleButton extends TextButton implements IToggleButton
	{
		public static const OFF_UP : String = "off_up";
		public static const OFF_OVER : String = "off_over";
		public static const OFF_DOWN : String = "off_down";
		public static const OFF_HIGHLIGHT : String = "off_over";
		public static const OFF_DISABLED : String = "off_disabled";
		internal static const OFF : String = "off_";
		
		protected var _sticky : Boolean;
		protected var _toggleActive : Boolean;
		
		/**
		 * <p>Get function for _toggleActive.</p>
		 * @return _toggleActive : Boolean
		 */
		public function get toggleActive() : Boolean {
			return _toggleActive;
		}

		/**
		 * <p>Set function for _toggleActive.</p>
		 * @param value : Boolean - true (Active) and false (Inactive)
		 */
		public function set toggleActive(value : Boolean) : void {
			_toggleActive = value;
			
			if(_sticky)	
				activate = !_toggleActive;
			
			updateToolTip();
			updateDisplay();
		}
		
		/**
		 * <p>Get function for _sticky.</p>
		 * @return _sticky : Boolean
		 */
		public function get sticky() : Boolean {
			return _sticky;
		}
	
		/**
		 * <p>Set function for _sticky.</p>
		 * @param value : Boolean
		 */
		public function set sticky(value : Boolean) : void {
			_sticky = value;
		}
		
		public function TextToggleButton(model:IModel = null)
		{
			super(model);
		}
		
		/**
		 * 
		 */
		override protected function init(model:IModel):void{
			super.init(model);
			
			sticky = (model["sticky"]) ? _typeHelper.stringToBoolean(model["sticky"]) : false;
		}
		
		/**
		 * 
		 * @param baseStyle
		 * @return 
		 * 
		 */		
		override protected function labelFieldStyleList(baseStyle:String):String{
			
			var allStyles:String = super.labelFieldStyleList(baseStyle);
			
			 allStyles +=  ",off_up:"+baseStyle+"-off_up,"+ 
					"off_over:"+baseStyle+"-off_hover,"+ 
					"off_down:"+baseStyle+"-off_down";
					
			return allStyles;
		}
		
		/**
		 * <p>Reverses the value of the toggle button.</p>
		 * @return toggle:Boolean - true for on, false for off
		 */
		public function toggle() : Boolean {
			
			toggleActive = !toggleActive;
			
			return toggleActive;
		}
		
		/**
		 * 
		 */
		public function updateDisplay() : void {
			changeState(UP);
		}
		
		/**
		 * 
		 */
		override public function changeState(value : String) : void {
			// This overrides the buttons default action of setting the display to an over state when pressed.
			// Looks to see if sticky and toggle is active then replaces the expected "over" state with an "up"
			// The up then goes through the regular filter of the toggleButton to apply off if in the toggle state.
			
			value = (_sticky && _toggleActive) ? UP : value;
			
			if(toggleActive)
				value = OFF + value;
			
			super.changeState(value);
			
		}
		
		/**
		 * 
		 */
		override public function clickAction(external:Boolean = false) : void {
			toggle();
			dispatchEvent(new ComponentEvent(ComponentEvent.TOGGLE_CHANGE, {action:action, value:toggleActive, external:external}, true, true));	
		}
	}
}